<?php namespace app\service\controller;
use think\Controller;
use think\Db;

/**
 * 注册服务层
 */
class Reg extends Controller{
	
	private $modelObj;				//模型句柄
	public function __construct(){
		parent::__construct();
		if(empty($this -> modelObj)) $this -> modelObj = new \app\model\model\SysUser;
	}
	
	/**
	 * 注册
	 */
	public function reg($in){
		if(empty($in)){
			$out_arr['code'] = '40004';
		}else{
			
//			调用ERP注册，目的获取SqlServer的ID
			$erpParam['UserName'] = $in['userName'];							//用户名就是手机号
			$erpParam['UserPwd'] = isset($in['userPwd']) ? $in['userPwd'] : md5(md5('123456') . config("ERP_AUTH_KEY"));	//密码
			$erpParam['ContactPhone'] = $in['userName'];						//联系电话
			$erpParam['Referee'] = '';											//推荐人电话
			$erpParam['Type'] = 5;												//写死
			$erpParam['ContactName'] = $in['nickName'];							//联系名称/昵称
			$erpParam['url'] = 'reg';											//ERP api地址
			
			$erpRe = controller("service/Erp") -> sendErp($erpParam);
			if($erpRe['erpReturnCode'] != '1'){
				$outArr['code'] = $erpRe['erpReturnCode'];
				$outArr['info'] = $erpRe;
				return $outArr;
			}
			
			$post['userName'] = isset($in['userName']) ? trim($in['userName']) : '';							//用户名就是手机号
			$post['userType'] = isset($in['userType']) ? trim($in['userType']) : '';							//用户类型（0：个人，1：企业）
//			$post['pwd'] = isset($in['pwd']) ? trim($in['pwd']) : md5(config("ERP_AUTH_KEY") . '123456');		//密码
			$post['pwd'] = isset($in['pwd']) ? trim($in['pwd']) : md5(config("ERP_AUTH_KEY") . '123456');		//密码
			$post['nickName'] = isset($post['nickName']) ? trim($post['nickName']) : '';						//昵称
			$post['sysUserSysID'] = $erpRe['Result']['UserID'];													//sysUser表sysID
			$post['wmsWarehouseuserSysID'] = $erpRe['Result']['WmsID'];											//wms_warehouseuser表sysID
			$post['wmsWarehousememberSysID'] = $erpRe['Result']['RefereeID'];									//wms_warehousemember表sysID
			$post['Referee'] = $erpRe['Result']['Referee'];														//推荐人电话
			$post['RefereeCode'] = $erpRe['Result']['RefereeCode'];												//推荐人用户名
			
//==========================================================写入SysUser=====================================================			
//			获取写入用户表的数据
			$newSysUserData = $this -> createUser($post);
			
			Db::startTrans();
			$sysUserRe = $this -> modelObj -> addData($newSysUserData);
			if(!$sysUserRe){
				Db::rollback();
				$outArr['code'] = '99997';
				$outArr['info'] = '新增sys用户失败';
				return $outArr;
			}
			$post['sysUserID'] = $sysUserRe;
			
//==========================================================写入userinfo=====================================================			
//			获取写入用户信息表数据
			$newUserInfo = $this -> createUserInfo($post);
			$userInfoModelObj = new \app\model\model\Userinfo;
			$userInfoRe = $userInfoModelObj -> addData($newUserInfo);
			if(!$userInfoRe){
				Db::rollback();
				$outArr['code'] = '99997';
				$outArr['info'] = '新增userInfo失败';
				return $outArr;
			}
			
//==========================================================写入WmsWarehouseuse供应商=====================================================	
//			获取写入供应商表数据
			$newWarehouseuser = $this -> createWarehouseuser($post);
			$wmsWarehouseuserModelObj = new \app\model\model\WmsWarehouseuser;
			$wmsWarehouseuserRe = $wmsWarehouseuserModelObj -> addData($newWarehouseuser);
			if(!$wmsWarehouseuserRe){
				Db::rollback();
				$outArr['code'] = '99997';
				$outArr['info'] = '新增wmsWarehouseuser失败';
				return $outArr;
			}

			
//==========================================================写入WmsWarehousemember用户关联表=====================================================	
//			获取写入用户关联表数据
			$newWarehousemember = $this -> createWarehousemember($post);
			$wmsWarehousememberModelObj = new \app\model\model\WmsWarehousemember;
			$wmsWarehousememberRe = $wmsWarehousememberModelObj -> addData($newWarehousemember);
			if(!$wmsWarehouseuserRe){
				Db::rollback();
				$outArr['code'] = '99997';
				$outArr['info'] = '新增wmsWarehousemember失败';
				return $outArr;
			}
			Db::commit();
			$outArr['code'] = '1';
		}
		return $outArr;
	}
	
	/**
	 * 创建用户表数据
	 */
	private function createUser($data){
		$newData['SysID'] = isset($data['sysUserSysID']) ? $data['sysUserSysID'] : '';				//SQLServer用户表ID
		$newData['UserName'] = isset($data['userName']) ? $data['userName'] : '';					//名称or手机号
		$newData['UserPwd'] = isset($data['pwd']) ? erpEncrypt($data['pwd']) : '';					//密码
		$newData['AddTime'] = date("Y-m-d H:i:s", time());											//添加时间
		$newData['Enable'] = 'U';																	//是否启用
		$newData['SystemID'] = 3;																	//系统ID？
		$newData['IsDelete'] = 'N';																	//是否删除
		$newData['IsManager'] = 'Y';																//是否管理员
		$newData['IsUseShop'] = 'N';																//是否使用商城
		return $newData;
	}
	
	/**
	 * 创建新用户信息数据
	 */
	private function createUserInfo($data){
		$newUserInfo['userID'] = isset($data['sysUserID']) ? $data['sysUserID'] : '';				//用户表ID
		$newUserInfo['userSign'] = '';		//作废
		$newUserInfo['address'] = '';		//作废
		$newUserInfo['address_id'] = '';	//作废
		$newUserInfo['sex'] = 0;			//性别
		$newUserInfo['age'] = 0;			//年龄
		$newUserInfo['user_type'] = isset($data['userType']) ? $data['userType'] : '';				//用户类型
		$newUserInfo['nickname'] = isset($data['nickName']) ? $data['nickName'] : '';				//用户昵称
		$newUserInfo['avatar_path'] = isset($data['avatar']) ? $data['avatar'] : '';				//头像
		return $newUserInfo;
	}

	/**
	 * 创建供应商表数据
	 */
	private function createWarehouseuser($data){
		$newWarehouseuser['SysID'] = isset($data['wmsWarehouseuserSysID']) ? $data['wmsWarehouseuserSysID'] : '';	//SQLServer表ID
		$newWarehouseuser['UserID'] = isset($data['sysUserSysID']) ? $data['sysUserSysID'] : '';					//SQLServer用户表ID
		$newWarehouseuser['WareHouseCode'] = $data['userName'];		//仓库简码
		$newWarehouseuser['WareHouseName'] = $data['nickName'];		//仓库名称
		$newWarehouseuser['WareHouseType'] = '';					//仓库类型
		$newWarehouseuser['WareHouseByName'] = $data['nickName'];	//仓库别名
		$newWarehouseuser['ContactName'] = $data['nickName'];		//联系人
		$newWarehouseuser['ContactPhone'] = $data['userName'];		//联系电话
		$newWarehouseuser['IsEnabled'] = 'Y';						//是否启用
		$newWarehouseuser['IsDelete'] = 'N';						//是否删除
		$newWarehouseuser['AddUserID'] = isset($data['sysUserSysID']) ? $data['sysUserSysID'] : '';					//SQLServer用户表ID
		$newWarehouseuser['AddUserName'] = '会员注册';				//
		$newWarehouseuser['AddTime'] = date("Y-m-d H:i:s", time());	//
		$newWarehouseuser['UpdateUserID'] = isset($data['sysUserSysID']) ? $data['sysUserSysID'] : '';				//SQLServer用户表ID
		$newWarehouseuser['UpdateUserName'] = '会员注册';			//
		$newWarehouseuser['UpdateTime'] = date("Y-m-d H:i:s", time());//
		$newWarehouseuser['IsLoaded'] = '';							//是否已经初始化库存完成(Y：是 N：否)
		$newWarehouseuser['Priority'] = 0;							//优先级
		$newWarehouseuser['Bail'] = 0;								//保证金（额度）
		$newWarehouseuser['Type'] = 5;								//会员类型
		$newWarehouseuser['WareHouseLevel'] = 0;					//等级
		$newWarehouseuser['WareHouseLevelID'] = 0;					//等级ID
		$newWarehouseuser['CurrentBail'] = 0;						//当前保证金
		$newWarehouseuser['RechargeBail'] = 0;						//充值保证金
		$newWarehouseuser['IsUseAdminLevel'] = '';					//是否使用主端等级
		$newWarehouseuser['IsHideAdminLevel'] = 0;					//是否隐藏主端等级
		$newWarehouseuser['WebUserID'] = isset($data['sysUserID']) ? $data['sysUserID'] : '';;						//用户表ID
		$newWarehouseuser['IsUseShop'] = 'N';						//是否启用商城
		return $newWarehouseuser;
	}

	/**
	 * 创建用户关联表数据
	 */
	private function createWarehousemember($data){
		$newWarehousemember['SysID'] = isset($data['wmsWarehousememberSysID']) ? $data['wmsWarehousememberSysID'] : '';	//SQLServer ID
		$newWarehousemember['MemberID'] = isset($data['sysUserSysID']) ? $data['sysUserSysID'] : '';					//SQLServer用户ID
		$newWarehousemember['WarehouseID'] = isset($data['wmsWarehouseuserSysID']) ? $data['wmsWarehouseuserSysID'] : '';	//SQLServer warehouseuserID
		$newWarehousemember['WX_OpenID'] = '';																//已关联的微信openid，已作废
		$newWarehousemember['Referee'] = isset($data['referee']) ? $data['referee'] : '';					//推荐人手机号
		$newWarehousemember['Referee_Code'] = isset($data['RefereeCode']) ? $data['RefereeCode'] : '';		//推荐人名称
		$newWarehousemember['WebUserID'] = isset($data['sysUserID']) ? $data['sysUserID'] : '';				//用户表ID
		$newWarehousemember['BelongWmsID'] = isset($data['wmsWarehouseuserSysID']) ? $data['wmsWarehouseuserSysID'] : '';	//SQLServer warehouseuserID
		$newWarehousemember['RefereeType'] = isset($data['refereeType']) ? $data['refereeType'] : '';		//推荐人类型 B/C
		return $newWarehousemember;
	}
}
